//
//  STStatusRetweetView.m
//  OCVVeibo
//
//  Created by leergou on 16/7/27.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "STStatusRetweetView.h"
#import "STStatusDTO.h"
#import "STStatusVM.h"
#import "STStatusPictureView.h"

static float Margin = 10;
static float cellWidth = 80;


@interface STStatusRetweetView ()

/** 转发微博内容 */
@property (nonatomic,strong) UILabel *contentLabel;
/** 配图 */
@property (nonatomic,strong) STStatusPictureView *pictureView;
/** 记录原创微博底部约束 */
@property (nonatomic,strong) MASConstraint *retweetViewBottomConstraint;

@end

@implementation STStatusRetweetView

- (instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 设置视图
- (void)setupUI{
//    self.backgroundColor = [UIColor redColor];
    
    // 添加子控件
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    
    // 添加约束
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self).offset(Margin);
    }];
    [self.pictureView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.bottom).offset(Margin);
        make.leading.equalTo((SCREEN_WIDTH - (3 * cellWidth + 2 * Margin)) * 0.5);
    }];
    
    // 设置 retweetView 自身的约束
    [self makeConstraints:^(MASConstraintMaker *make) {
        self.retweetViewBottomConstraint =  make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
    }];
}

#pragma mark - 重写 statusVM 模型set 方法,分发数据
- (void)setStatusVM:(STStatusVM *)statusVM{
    _statusVM = statusVM;
    self.contentLabel.text = statusVM.status.retweeted_status.text;
    
    //    NSLog(@"%ld",status.retweeted_status.pic_urls.count);
    
    // 判断是否有配图
    // 首先卸载约束 - retweetViewBottomConstraint
    [self.retweetViewBottomConstraint uninstall];
    if (statusVM.status.retweeted_status.pic_urls.count > 0) {
        // 首先赋值
        self.pictureView.statusPictureList = statusVM.status.retweeted_status.pic_urls;
        // 更新约束
        [self makeConstraints:^(MASConstraintMaker *make) {
            self.retweetViewBottomConstraint = make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
        }];
        // 显示配图
        self.pictureView.hidden = NO;
    } else {
        // 更新约束
        [self makeConstraints:^(MASConstraintMaker *make) {
            self.retweetViewBottomConstraint = make.bottom.equalTo(self.contentLabel.bottom).offset(Margin);
        }];
        // 隐藏配图
        self.pictureView.hidden = YES;
    }

    
}
//
//#pragma mark - 重写模型set 方法,分发数据
//- (void)setStatus:(STStatusDTO *)status{
//    _status = status;
//    self.contentLabel.text = status.retweeted_status.text;
//    
////    NSLog(@"%ld",status.retweeted_status.pic_urls.count);
//    
//    // 判断是否有配图
//    // 首先卸载约束 - retweetViewBottomConstraint
//    [self.retweetViewBottomConstraint uninstall];
//    if (status.retweeted_status.pic_urls.count > 0) {
//        // 首先赋值
//        self.pictureView.statusPictureList = status.retweeted_status.pic_urls;
//        // 更新约束
//        [self makeConstraints:^(MASConstraintMaker *make) {
//            self.retweetViewBottomConstraint = make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
//        }];
//        // 显示配图
//        self.pictureView.hidden = NO;
//    } else {
//        // 更新约束
//        [self makeConstraints:^(MASConstraintMaker *make) {
//            self.retweetViewBottomConstraint = make.bottom.equalTo(self.contentLabel.bottom).offset(Margin);
//        }];
//        // 隐藏配图
//        self.pictureView.hidden = YES;
//    }
//}


#pragma mark - lazy
- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] textFont:15 maxWidth:(SCREEN_WIDTH - 2 * Margin)];
    }
    return _contentLabel;
}

- (STStatusPictureView *)pictureView{
    if (!_pictureView) {
        _pictureView = [[STStatusPictureView alloc] init];
    }
    return _pictureView;
}






@end




















